//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

#endregion

namespace OpenLS.Spreadsheet.UI.Internals
{
    internal static class CellTextEditorTyping
    {
        internal static void RegisterClassHandlers(Type controlType)
        {
            CanExecuteRoutedEventHandler canExecuteRoutedEventHandler = onQueryStatus;

            CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.Delete, onDelete,
                                                  canExecuteRoutedEventHandler, Key.Delete);
            CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.Backspace, onBackspace,
                                                  canExecuteRoutedEventHandler, Key.Back);
            CommandHelpers.RegisterCommandHandler(controlType, SpreadsheetEditingCommands.ToggleEditionMode, onToggleLockReference,
                                                  canExecuteRoutedEventHandler, Key.F4);
            EventManager.RegisterClassHandler(controlType, TextCompositionManager.TextInputEvent,
                                              new TextCompositionEventHandler(onTextInput));
            CommandHelpers.RegisterCommandHandler(controlType, SpreadsheetCommands.SelectRow, OnSelectRow,
                                                  (o, args) => {
                                                                   args.CanExecute = true;
                                                                   args.Handled = true;
                                                  }, Key.Space, ModifierKeys.Shift);
        }

        private static void OnSelectRow(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;
            tb.Source.OnTextInput(" ");
            e.Handled = true;
        }


        private static void onTextInput(object sender, TextCompositionEventArgs e)
        {
            var tb = (CellTextBox) sender;
            tb.Source.OnTextInput(e.Text);
            e.Handled = true;
        }

        private static void onBackspace(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;

            tb.Source.OnBackspace();
            e.Handled = true;
        }
        private static void onToggleLockReference(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;
            tb.Source.OnToggleLockReference();
            e.Handled = true;
        }

        private static void onDelete(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox) sender;
            tb.Source.OnDelete();
            e.Handled = true;
        }

        private static void onQueryStatus(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }
    }
}